package 面试题;

import java.util.concurrent.CountDownLatch;

public class 多线程先打印5个hello在打印5个world{
    private static volatile int i = 0;
    private static CountDownLatch latch = new CountDownLatch(5);

    static class Print implements Runnable{
        Object lock;

        public Print(Object lock) {
            this.lock = lock;
        }

        @Override
        public void run() {
            System.out.println("hello");
            latch.countDown();
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("world");
        }

//        @Override
//        public void run() {
//            synchronized (lock){
//                System.out.println("hello");
//                if (++i < 5){
//                    try {
//                        lock.wait();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }else {
//                    lock.notifyAll();
//                }
//                System.out.println("world");
//            }
//
//        }
//        @Override
//        public void run() {
//            System.out.println("hello");
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("world");
//        }
    }

    public static void main(String[] args) {
        Object lock = new Object();
        for (int i = 0; i < 5; i++) {
            new Thread(new Print(lock)).start();
        }

        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                System.out.println("123");
            }).start();
        }
    }

}
